local blaze = require "blaze"
local game = require "club.threecard.scibo"
local consts = game.util.consts

local M = game.ui.activity()

local function tween_extended_view(self)
    self.view_rect:DOAnchorPosY(-(268 - self.border_rect.sizeDelta.y), 0.5)
    game.coroutine.wait(5)
    self.view_rect:DOAnchorPosY(0 , 0.5)
end

M.store_watchers = {
    gameplay = {
        ["$"] = blaze.noop,

        ["展示结果"] = function(self, store, state, args)
            self:start_coroutine(function()
                --todo
                self.bet_area:result(state.result_view.hits)
            end)
        end,

        ["阶段变更"] = function(self, store, state, args)
            if blaze.screen.ratio <= 4 / 3 then
                return
            end
            if state.phase_id == consts.PHASE_IDLE then
                self:start_coroutine(tween_extended_view, self)
            elseif state.phase_id == consts.PHASE_BET then
                self.view_rect:DOAnchorPosY(0 , 0.5)
            elseif state.phase_id == consts.PHASE_REVEAL then
                self:start_coroutine(tween_extended_view, self)
            end
        end
    },

    setting = {
        ["屏幕变更"] = function(self, store, state, args)
            local ratio = blaze.screen.ratio
            local height = blaze.screen.height
            self.view_rect.sizeDelta = CS.UnityEngine.Vector2(1440, 1440 / ratio)

            local area_bg_scale
            if ratio > 2 then
                area_bg_scale = math.max(0.8, 1 - (ratio - 2) * 0.5)
            else
                area_bg_scale = 1
            end
            self.area_bg_root.localScale = CS.UnityEngine.Vector3(area_bg_scale, area_bg_scale, area_bg_scale)
        end
    }
}

function M:ctor()
    self.res = "club.threecard.scibo/ui/ingame.b:ingame"

    local dice_shower = game.require "ui.ingame.dice_shower"
    self.dice_shower = dice_shower.new()

    local player_info = require "club.threecard.gamelib.widget.player_info"
    self.player_info = player_info.new()

    local BetOperator = require "club.threecard.baccaratlib.ui.widgets.BetOperator"
    self.bet_operator = BetOperator.new()

    local PhaseShower = require "club.threecard.baccaratlib.ui.widgets.PhaseShower"
    self.phase_shower = PhaseShower.new()

    local Resumer = require "club.threecard.baccaratlib.ui.widgets.Resumer"
    self.resumer = Resumer.new(self)

    local Seats = require "club.threecard.baccaratlib.ui.widgets.Seats"
    self.seats = Seats.new(0, "club.threecard.scibo/ui/ingame.b:seats")

    local Others = require "club.threecard.baccaratlib.ui.widgets.Others"
    self.others = Others.new(self.seats)

    local BetAreas = require "club.threecard.baccaratlib.ui.gameplay.BetAreas"
    self.bet_area = BetAreas.new("club.threecard.scibo/ui/ingame.b:bet_area")

    local BetChip = require "club.threecard.baccaratlib.ui.gameplay.BetChip"
    self.bet_chip = BetChip.new(self.bet_area, self.seats, self.others, "club.threecard.scibo/ui/ingame.b:bet_chip")

    local rule = game.require "ui.ingame.rule"
    self.rule = rule.new()
end

function M:load()
    self:super("load")

    self.dice_shower:load()
    self.dice_shower:set_parent(self.dice_shower_root, false)

    self.player_info:load()
    self.player_info:set_parent(self.self_root, false)

    self.bet_operator:load()
    self.bet_operator:set_parent(self.bet_operator_root, false)

    self.phase_shower:load()
    self.phase_shower:set_parent(self.phase_shower_root, false)

    self.others:load()
    self.others:set_parent(self.others_root, false)

    self.seats:load()
    self.seats:set_parent(self.seats_root, false)

    self.bet_area:load()
    self.bet_area:set_parent(self.bet_area_root, false)

    self.bet_chip:load()
    self.bet_chip:set_parent(self.bet_chip_root, false)

    self.rule:load()
    self.rule:set_parent(self.rule_root, false)

    self.resumer:load()
    self.resumer:set_parent(self.resumer_root, false)
end

function M:on_injected(bind)
    bind("button.click", self.btn_exit, self.exit_ivk)
    bind("shortcut.escape", self.btn_exit.gameObject, self.exit_ivk)
    bind("button.click", self.btn_rule, self.rule_ivk)
    bind("button.click", self.btn_audio_off, self.audio_off_ivk)
    bind("button.click", self.btn_audio_on, self.audio_on_ivk)

    game.utility.button_style.highlight_scale(self.btn_exit)
    game.utility.button_style.highlight_scale(self.btn_rule)
    game.utility.button_style.highlight_scale(self.btn_audio_off)
    game.utility.button_style.highlight_scale(self.btn_audio_on)
end

function M:exit_ivk()
    game.game_scene.leave()
end

function M:rule_ivk()
    self.rule:show()
end

function M:audio_off_ivk()
    -- game.toast.show("????")
end

function M:audio_on_ivk()
    -- game.toast.show("????")
end

return M